﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Computer_Manager.DTO;

namespace Computer_Manager.DAL
{
    public class DAL_customer
    {
        DbProvider db = new DbProvider();
        SqlCommand cmd;

        public void customer_insert(DTO_customer dt)
        {
            db.Open();
            cmd = new SqlCommand("customer_Insert", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@name_cus", SqlDbType.NVarChar).Value = dt.name_cus;
            cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = dt.address;
            cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = dt.phone;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = dt.email;
            cmd.Parameters.Add("@tax", SqlDbType.NVarChar).Value = dt.tax;
            cmd.Parameters.Add("@deputy", SqlDbType.NVarChar).Value = dt.deputy;
            cmd.Parameters.Add("@identity_card", SqlDbType.Int).Value = dt.identity_card;
            cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = dt.type;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }

        public void customer_Update(DTO_customer dt)
        {
            db.Open();
            cmd = new SqlCommand("customer_Update", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cus", SqlDbType.Int).Value = dt.id_cus;
            cmd.Parameters.Add("@name_cus", SqlDbType.NVarChar).Value = dt.name_cus;
            cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = dt.address;
            cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = dt.phone;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = dt.email;
            cmd.Parameters.Add("@tax", SqlDbType.NVarChar).Value = dt.tax;
            cmd.Parameters.Add("@deputy", SqlDbType.NVarChar).Value = dt.deputy;
            cmd.Parameters.Add("@identity_card", SqlDbType.Int).Value = dt.identity_card;
            cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = dt.type;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void customer_Delete(DTO_customer dt)
        {
            db.Open();
            cmd = new SqlCommand("customer_Delete", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cus", SqlDbType.Int).Value = dt.id_cus;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            db.getConn().Close();
        }
        public DataTable GetAll()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("customer_SelectAll", db.getConn());
            DataTable table = new DataTable();
            adapter.Fill(table);
            adapter.Dispose();
            return table;
        }
        public DataTable GetByID(DTO_customer dt)
        {
            cmd = new SqlCommand("customer_GetByID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cus", SqlDbType.Int).Value = dt.id_cus;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetByType(DTO_customer dt)
        {
            cmd = new SqlCommand("customer_GetByType", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = dt.type;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
    }
}
